/*
 * @Author: humengchuan 531537052@qq.com
 * @Date: 2024-09-10 18:26:40
 * @LastEditors: humengchuan 531537052@qq.com
 * @LastEditTime: 2025-10-24 15:08:55
 * @FilePath: \work-tool\experi-api\app.js
 * @Description: 
 */
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const cors = require('cors')
/*
 * graphql 配置
 */ 
const { graphqlHTTP } = require('express-graphql')
const graphqlConf = require('./grapql/schemaConfig')


var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var apiRouter = require('./routes/api/index');
var customRouter = require('./routes/custom/index');
var v1ApiRouter = require('./routes/v1');
var v1RegisterRouter = require('./routes/v1/register');
var dynamicRoutes = require('./routes/dynamic-routes');
// var svgVerifyRouter = require('./middlewares/svg-vertfi');


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

/**跨域配置*/
const corsOptions = {
  // origin: ['http://localhost:3000', 'http://localhost:8080', 'http://192.168.1.15:3000', http://8.148.64.152/], // 允许跨域的域名，可以用*表示允许任何域名
  origin: '*', // 允许跨域的域名，可以用*表示允许任何域名
  methods: ['GET', 'POST', 'PUT', 'DELETE']
};
app.use(cors(corsOptions))

// 路由
app.use('/', indexRouter);
app.use('/users', usersRouter);

app.use(apiRouter);
app.use(customRouter);
app.use('/v1', v1ApiRouter);
app.use('/v1', v1RegisterRouter);
app.use(dynamicRoutes);
// app.use('/v1', svgVerifyRouter);

// graphql 接口
app.use('/graphql', graphqlHTTP({
  schema: graphqlConf.schema,
  rootValue: graphqlConf.root,
  graphiql: true,
}))

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
